package com.touch.jgloo.owt;
import com.touch.jgloo.core.*;
/**
 * A JGLButton is a JGLComponent that listens for a Mouse event. 
 * When that mouse event occurs within its field, it then pipes the event to another mouse listener.
 * A JGLButton is displayed as a background, a foreground, and a text caption.
 * @author Homicidal Monkey
 * @version 10.1.9
 * @since r419
 * @see com/touch/Licence
 */
public final class JGLButton extends JGLComponent
{
	//GLObject used for the background
	private final JGLQuad background;
	//GLObject used for the foreground
	private final JGLQuad foreground;
	//GLObject used for the caption
	private final JGLTextObject caption;
	/**
	 * Construct the JGLButton with a specified caption.
	 * This will use the default size of 128x64
	 * @param caption Caption to be displayed on the button.
	 * @since r461
	 */
	public JGLButton(final String caption)
	{
		//create the background object
		this.background = new JGLQuad(128, 64);
		//create the foreground object
		this.foreground = new JGLQuad(124, 60);
		//create the caption object		
		this.caption = JGLTextObject.createTextObject(caption, JGLComponent.TEXT_TYPE);		
		//apply the style sheet. Use the panel default
		this.applyStyleSheet(JGLStyleSheet.DEFAULT_PANEL);
	}
	/**
	 * Build the JGLButton. Set the caption and define the size.
	 * @param caption Caption of the JGLButton
	 * @param w Width of the JGLButton
	 * @param h Height of the JGLButton
	 * @since r461
	 */
	public JGLButton(final String caption, final int w, final int h)
	{
		//build the background object
		this.background = new JGLQuad(w, h);
		//build the foreground object
		this.foreground = new JGLQuad(w - 4, h - 4);
		//build teh caption object
		this.caption = JGLTextObject.createTextObject(caption, JGLComponent.TEXT_TYPE);
		//set the size
		this.setSize(w, h);
		//apply the style sheet. use the default panel 
		this.applyStyleSheet(JGLStyleSheet.DEFAULT_BUTTON);
	}	
	/**
	 * Sets the caption of the JGLButton
	 * @param caption Caption to be displayed on the JGLButton.
	 * @since r461
	 */
	public final  void setCaption(final String caption)
	{
		//set the caption
		this.caption.setCaption(caption);
	}
	/**
	 * Gets the caption of the JGLButton
	 * @return Caption displayed on the JGLButton as a String
	 * @since r461
	 */
	public final  String getCaption()
	{
		//get the caption
		return this.caption.getCaption();
	}	
	
	@Override
	public final  void applyStyleSheet(final JGLStyleSheet ss) 
	{
		this.background.setColor(ss.backgroundColor);
		this.background.setTexture(ss.backgroundTexture);
		this.background.setUV(JGLUVType.QUADMAP);
		this.foreground.setColor(ss.foregroundColor);
		this.foreground.setTexture(ss.foregroundTexture);
		this.foreground.setUV(JGLUVType.QUADMAP);
		this.caption.setTexture(ss.componentBackgroundTexture);
		this.caption.setColor(ss.componentBackgroundColor);				
	}
	public final  void resized(final float w, final float h)
	{
		foreground.resize(w - 4, h - 4);
		background.resize(w, h);	
	}	
	public final  void repositioned(final float x, final float y, final float z){}	
	public final void render()
	{
		//confirm that the object exists
		if(this.isVisible() == false)
			return;
		//push the matrix up one
		GL_INSTANCE.glPushMatrix();
			//translate to the object's position
			GL_INSTANCE.glTranslatef(this.getPosition().x, this.getPosition().y, this.getPosition().z);
			//render the background object
			this.background.render();
				//push the matrix up one
				GL_INSTANCE.glPushMatrix();
				//translate offset
				GL_INSTANCE.glTranslatef(2, 2, 0);
				//render the foreground
				this.foreground.render();
			//push the matrix down one
			GL_INSTANCE.glPopMatrix();
			//translate to the Caption's position
			GL_INSTANCE.glTranslatef(this.getSize().x / 2 - this.caption.length() * 5, this.getSize().y / 2 - 10, 0);
			//render the caption
			this.caption.render();
		//push the matrix down one
		GL_INSTANCE.glPopMatrix();
	}	
	public final  void clean() 
	{
		//clean the background
		this.background.clean();
		//clean the foreground
		this.foreground.clean();		
	}
	public final  void compile()
	{
		//compile the background
		this.background.compile();
		//compile the foreground
		this.foreground.compile();
	}
}
